/**
 * 
 */
package com.HibernateRecipes.Chapter2.DAO;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.Hib.Util.SessionManager;
import com.HibernateRecipes.Chapter2.domain.Book2;

/**
 * @author Ignatiues charles arun
 * 
 *         Nov 29, 2010
 */
public class BookDAO_CH2
	{

		private static SessionFactory sessionFactory;

		/**
		 * @param args
		 */
		public static void main(String[] args)
			{
				// TODO Auto-generated method stub

				sessionFactory = getSession();
				Session session = sessionFactory.getCurrentSession();
				session.beginTransaction();
				Book2 bookCH2 = new Book2();
				bookCH2.setName("Spring Recepis");
				session.saveOrUpdate(bookCH2);
				session.getTransaction().commit();
				List<Book2> bookValues = getBook();
				Iterator<Book2> itr = bookValues.iterator();

				while (itr.hasNext())
					{
						Book2 obj = itr.next();
						System.out.print(obj.getIsbn() + "\t");
						System.out.print(obj.getName() + "\t");
						System.out.println(obj.getPublishDate());

					}

				// session.close();

			}

		private static SessionFactory getSession()
			{
				sessionFactory = SessionManager.getSessionfactory();
				return sessionFactory;

			}

		@SuppressWarnings("unchecked")
		private static List<Book2> getBook()
			{
				Session session = sessionFactory.getCurrentSession();
				session.beginTransaction();
				return (session.createQuery("from bkch2").list());

			}

	}
